package com.xxx.datasources;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;

/**
 * 配置多数据源
 * @author Flying
 * @email flying_miku@sina.com
 * @date 2017/8/19 0:41
 */
@Configuration
@EnableTransactionManagement
public class DynamicDataSourceConfig implements TransactionManagementConfigurer {

    @Autowired
    private PlatformTransactionManager mainTransactionManagement;



    @Bean
    @Primary
    public PlatformTransactionManager transactionalManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }




    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return mainTransactionManagement;
    }
}
